package com.geekband.Test01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Function:
 * Create date on 15/11/4.
 *
 * @author Conquer
 * @version 1.0
 */
public class ListViewDemoActivity extends Activity {

    private ListView mPhoneBookListView;
    private List<UserInfo> mUserInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_demo);

        mPhoneBookListView = (ListView) findViewById(R.id.list_view);
        mUserInfos = new ArrayList<>();
        mUserInfos.add(new UserInfo("刘小明", 21));
        mUserInfos.add(new UserInfo("张大伟", 22));
        mUserInfos.add(new UserInfo("宋小宝", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("刘小明", 21));
        mUserInfos.add(new UserInfo("张大伟", 22));
        mUserInfos.add(new UserInfo("宋小宝", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("刘小明", 21));
        mUserInfos.add(new UserInfo("张大伟", 22));
        mUserInfos.add(new UserInfo("宋小宝", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));
        mUserInfos.add(new UserInfo("张小红", 21));

        final PhoneBookAdapter phoneBookAdapter = new PhoneBookAdapter(ListViewDemoActivity.this, mUserInfos);

        mPhoneBookListView.setAdapter(phoneBookAdapter);
        mPhoneBookListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    // 新建另外一批数据
                    // 替换掉老的数据
                    // 刷新listview，让它更新自己的视图
                    mUserInfos.clear();
                    mUserInfos.add(new UserInfo("我是新的数据一", 1892));
                    mUserInfos.add(new UserInfo("我是新的数据二", 2345));
                    mUserInfos.add(new UserInfo("我是新的数据三", 3245));

                    phoneBookAdapter.refreshData(mUserInfos);
                    phoneBookAdapter.notifyDataSetChanged();
                }

                Toast.makeText(ListViewDemoActivity.this, mUserInfos.get(position).getUserName() + "被我点击了，怎么办？", Toast.LENGTH_LONG).show();
            }
        });

        //

        mPhoneBookListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ListViewDemoActivity.this, mUserInfos.get(position).getUserName() + "被我长按了，怎么办？", Toast.LENGTH_LONG).show();
                return false;
            }
        });
    }
}
